<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Gallery/classes/PhpFox_ComponentBattle.class.php, $Author:  $ */


/** Component for battle page
 *
 * @version $Id: PhpFox_ComponentBattle.class.php 313 2008-02-12 10:04:00Z phpfoxsv $
 * @package module.gallery
 */
class PhpFox_Mod_Gallery_ComponentBattle extends BaseComponent
{
    function process()
    {
        $oGalleryItem = $this->_oModule->getItem('Gallery');
        /* @var $oGalleryItem PhpFox_Mod_Gallery_ItemGallery */
        $oGalleryService = $this->_oModule->getService('Gallery');
        /* @var $oGalleryService PhpFox_Mod_Gallery_ServiceGallery */
        $oSecurityService = App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        App::loadClass('Filter');
        $oReq = &App::getRequest();
        $oTpl = $this->_oModule->getTemplate();
        $aOut = array();

        $oSecurityService->checkPerm('gallery.vote', 'full');
        $oSecurityService->checkAccess('battle');
    	$aCategories = array();
        $aCategories[''] = App::format('option.select');
		foreach (App::getOptions('image_cat') as $sCategoryId => $sCategoryText)
        {
            $aCategories[$sCategoryId] = $sCategoryText;
        }

        $aCond = array
        (
        	'category' => array
        	(
        		'value'  => '=',
        		'select' => $aCategories,
        	),
        );
        $oFilter = &new Filter('battle', $aCond);
        $aCond = array();
        foreach ($oFilter->aConditions as $sCond)
        {
            $aCond[] = $sCond;
        }
        $aCond[] = 'ma.allow = ""';
        $aCond[] = 'ma.who_view = ""';
        $bIsFailed = false;
        if ($iId = $oReq->getInt('id'))
        {
        	if ( isset($_SESSION['gallery_last_voted']) && $_SESSION['gallery_last_voted'] == $iId )
        	{
        		$bIsFailed = true;
        	}
        	if ( !$bIsFailed )
        	{
        		$oGalleryItem->load($iId);
        		if ($oGalleryItem->aData['who_view'] == '0')
        		{
    			    $oGalleryItem->aData['faceoff']++;
    			    $oGalleryItem->update(array('faceoff'));
    			    $_SESSION['gallery_last_voted'] = $iId;
        		}
            	$oReq->gotoUrl('public.gallery.battle');
			}
        }

        $sOrder = 'RAND()';

        list($aItems, $iCnt) = $oGalleryItem->getList($aCond, 1, 1, $sOrder, '', '', 'ma.id');

        $oTpl->assign(array(
        	'aFilter'     => $oFilter->aHtml,
        	'sFilterName' => 'battle',
        ));

		if ($iCnt >= 2)
		{
		    $iId1 = $aItems[0]['id'];
		    $aCond['id'] = '<> '.$iId1;

		    list($aItems, $iCnt) = $oGalleryItem->getList($aCond, 1, 1, $sOrder, '', '', 'ma.id');
		    
			$sActualImage1 = App::getParam('gallery.pic.url') . $iId1 . '_view.jpg';
			if (!file_exists(App::getParam('gallery.pic.path') . $iId1 . '_view.jpg'))
			{
				$sActualImage1 = App::getParam('gallery.pic.url') . $iId1 . '.jpg';
			}

			$sActualImage2 = App::getParam('gallery.pic.url') . $aItems[0]['id'] . '_view.jpg';
			if (!file_exists(App::getParam('gallery.pic.path') . $aItems[0]['id'] . '_view.jpg'))
			{
				$sActualImage2 = App::getParam('gallery.pic.url') . $aItems[0]['id'] . '.jpg';
			}

		    $oTpl->assign(array(
		    	'sActualImage1' => $sActualImage1,
		    	'sActualImage2' => $sActualImage2,
		    	'iId1'       => $iId1,
		    	'iId2'       => $aItems[0]['id'],
		    	'sGImageUrl' => App::getParam('gallery.pic.url'),
		    ));
		}
		else
		{
		    $oTpl->assign('bNoPics', true);
		}


		$aOut['main']   = $oTpl->fetch('Battle.html');
		$aOut['filter'] = $oTpl->fetch('GroupFilter.html');
		return $aOut;
    }
}
?>